
// NOTA: funcoes a rever (variaveis de configuração, comentar, etc)




/**
 * domains_main()
 *
 * Initialization function for domains window
 *
 */

function domains_main()
{
	// Fill servers drop list
	domains_getServers();

	// Fill domains list box
	domains_getDomains();
}

/**
 * domains_getServers()
 *
 * Fill drop list with available servers
 *
 */
 
function domains_getServers()
{
	// We assume only one available hardcoded server
	document.getElementById('serversMenu').appendItem('server2.openquest.pt');
	document.getElementById('serversMenu').currentItem = 0;
}

/**
 * domains_getAccounts()
 *
 * Get accounts for selected domain
 *
 */
 
function domains_getAccounts()
{
	alert(1);
	var xmlrpcCall = makeXMLRpcCall('mail.listDomainUsers', ['OpenQuest',document.getElementById('domainsList').currentItem.label,0,-1]);
	var result;
	xmlrpcCall.sendRequest(SERVER_URL, getAccounts_response);
	disable_ctrls();
}

function getAccounts_response(response)
{
	listClear('accountsList');
	var data = parseResult(response);
	for (var i=0; i<data.length; i++)
		document.getElementById('accountsList').appendItem(data[i]);
		
	if (data.length==0)
		document.getElementById('accountsList').appendItem("<no accounts defined>");

	document.getElementById('accountsList').ensureIndexIsVisible(0);
	document.getElementById('accountsList').selectItem(document.getElementById('accountsList').getItemAtIndex(0));	
	enable_ctrls();
}

/**
 * domains_getDomains()
 *
 * Get domains (for selected server - future)
 *
 */

function domains_getDomains()
{
	var xmlrpcCall = makeXMLRpcCall('mail.listDomains', ['OpenQuest',0,-1]);
	var result;
	xmlrpcCall.sendRequest(SERVER_URL, getDomains_response);
	disable_ctrls();
}

function getDomains_response(response)
{
	listClear('domainsList');
	var data = parseResult(response);
	for (var i=0; i<data.length; i++)
		document.getElementById('domainsList').appendItem(data[i]);
	document.getElementById('domainsList').ensureIndexIsVisible(0);
	document.getElementById('domainsList').selectItem(document.getElementById('domainsList').getItemAtIndex(0));	
	enable_ctrls();
}

/**
 * domains_addDomain()
 *
 * Open window to add domain form
 *
 */

function domains_addDomain()
{
	result = prompt("New domain name");
	if (result != null)
	{
		disable_ctrls();
		var xmlrpcCall = makeXMLRpcCall('mail.newDomain', ['OpenQuest',result]);
		xmlrpcCall.sendRequest(SERVER_URL, addDomain_response);
	}
}

function addDomain_response(response)
{
	alert("Domain sucessfully added");
	enable_ctrls();
	domains_getDomains();
}

/**
 * domains_addAccount()
 *
 * Open window to add account form
 *
 */

function domains_addAccount()
{
	if (document.getElementById('domainsList').selectedIndex == -1)
	{
		alert("No domain selected. Please select a domain.");
		return -1;
	}

	result = prompt("New account name");
	if (result != null)
	{
		disable_ctrls();
		var xmlrpcCall = makeXMLRpcCall('mail.newUser', ['OpenQuest',document.getElementById('domainsList').currentItem.label,result]);
		xmlrpcCall.sendRequest(SERVER_URL, addAccount_response);
	}
}

function addAccount_response(response)
{
	var data = parseResult(response);
	
	enable_ctrls();

	if (data == -1)
		alert("Cannot add account. Please verify account name.");
	else
		domains_getAccounts();
}


/**
 * domains_setAccountPass()
 *
 * Open window to change account's password form
 *
 */

function domains_setAccountPass()
{
	if (document.getElementById('accountsList').selectedIndex == -1)
	{
		alert("No account selected. Please select an account.");
		return -1;
	}

	result = prompt("New account password");
	if (result != null)
	{
		disable_ctrls();
		var xmlrpcCall = makeXMLRpcCall('mail.setPass', ['OpenQuest',document.getElementById('accountsList').currentItem.label,result]);
		xmlrpcCall.sendRequest(SERVER_URL, setPass_response);
	}
}

function setPass_response(response)
{
	alert("Password changed");
	enable_ctrls();
}

/**
 * domains_delDomain()
 *
 * Remove domain from server
 *
 */

function domains_delDomain()
{
	if (document.getElementById('domainsList').selectedIndex == -1)
	{
		alert("No domain selected. Please select a domain.");
		return -1;
	}

	if (!confirm("Really remove domain \""+document.getElementById('domainsList').currentItem.label+"\"?"))
		return 0;
		
	var xmlrpcCall = makeXMLRpcCall('mail.delDomain', ['OpenQuest',document.getElementById('domainsList').currentItem.label]);
	var result;
	xmlrpcCall.sendRequest(SERVER_URL, delDomain_response);
	disable_ctrls();
}

function delDomain_response(response)
{
	var data = parseResult(response);
	enable_ctrls();
	if (data == -1)
		alert("Cannot remove domain. Domain has accounts configured.");
	else
	{
		domains_getDomains();
		alert("Domain removed with sucess.");
	}
}

/**
 * domains_delAccount()
 *
 * Remove account from server
 *
 */

function domains_delAccount()
{
	if (document.getElementById('accountsList').selectedIndex == -1)
	{
		alert("No account selected. Please select an account.");
		return -1;
	}

	if (!confirm("Really remove account \""+document.getElementById('accountsList').currentItem.label+"\"?"))
		return 0;
		
	var xmlrpcCall = makeXMLRpcCall('mail.delUser', ['OpenQuest',document.getElementById('accountsList').currentItem.label]);
	var result;
	xmlrpcCall.sendRequest(SERVER_URL, delAccount_response);
	disable_ctrls();
}

function delAccount_response(response)
{
	alert("Account removed with sucess.");
	enable_ctrls();
	domains_getAccounts();
}

